<div class="templatemo-content-wrapper">
	<div class="templatemo-content">
		<ul class="nav nav-tabs mytabs">
      <li class="active"><a href="<?=site_url()?>/user/index">用户列表</a></li>
		</ul>
		<div class="row">

			<div class="col-md-12">
				<div class="table-responsive">
					<h4 class="margin-bottom-15">编辑用户</h4>
					<form role="form" id="templatemo-preferences-form"
						onsubmit="return false;">
						<div class="row">
							<input type="hidden" name="userid" value="<?=$user['id']?>">
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">手机号</label> <input
								  type="text" class="form-control" id="name" name="phone" value="<?=$user['phone']?>">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">家长名</label> <input
									type="text" class="form-control" id="description" name="pname" value="<?=$user['pname']?>">
							</div>
						</div>
						<div class="row">
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">学生名</label> <input
									type="text" class="form-control" id="name" name="sname" value="<?=$user['sname']?>">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">学校</label>
								<select name="" id="school" class="form-control">
									<?php foreach ($school as $item) { ?>
									<option value="<?=$item['id']?>-<?=$item['type']?>" <?php if($user['school']==$item['id']){echo 'selected';} ?>><?=$item['name']?></option>
									<?php } ?>
								</select>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">年级</label>
								<select name="" id="grade" class="form-control">
									<?php foreach ($grade as $item) { ?>
									<option value="<?=$item['id']?>" <?php if($user['grade']==$item['id']){echo 'selected';} ?>><?=$item['name']?></option>
									<?php } ?>
								</select>
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">班级</label>
								<select name="classes" id="class" class="form-control">
									<?php foreach ($class as $item) { ?>
									<option value="<?=$item['id']?>" <?php if($user['class']==$item['id']){echo 'selected';} ?>><?=$item['name']?></option>
									<?php } ?>
								</select>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">学号</label> <input
									type="text" class="form-control" id="name" name="sno" value="<?=$user['sno']?>">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">卡号1</label> <input
									type="text" class="form-control" id="no" name="no" value="<?=$user['no']?>">
							</div>

						</div>
						<div class="row">

							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">卡号2</label> <input
									type="text" class="form-control" id="no2" name="no2" value="<?=$user['no2']?>">
							</div>

						</div>
						<div class="row templatemo-form-buttons">
							<div class="col-md-12">
								<button type="submit" class="btn btn-primary" id="submit_btn">编辑</button>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
var schoolid = 0;
	$(document).ready(function() {
		$('body').on('click', '#submit_btn', function(){


			if($('#class').val() == '' || $('#class').val() == undefined ||  $('#class').val() == null || $('#class').val() == 0){
				alert("学校信息不能为空")
				return;
			}
			if($('input[name="sno"]').val() == ''){
				alert("学号不能为空")
				return
			}
			var req = /^[1-9]{1}\d{0,1}$/;
			if(!req.test($('input[name="sno"]').val())){
				//alert('学号格式错误，必须为小于三位的整数')
				//return ;
			}
			$.ajax({
				url: "<?=site_url('/user/update')?>",
				type: 'post',
				data: {
					phone: $('input[name="phone"]').val(),
					userid: $('input[name="userid"]').val(),
					username: $('input[name="username"]').val(),
					pname: $('input[name="pname"]').val(),
					sname: $('input[name="sname"]').val(),
					class: $('#class').val(),
					sno: $('input[name="sno"]').val(),
					no: $('input[name="no"]').val(),
					no2: $('input[name="no2"]').val()

				},
				success: function(d){
					if(d.code){
						alert('编辑成功');
						window.history.go(-1)
					} else {
						alert(d.msg)
					}
				},
				error: function(){

				},
				dataType: 'json'
			})
		})

		 $('#school').change(function(){
		 	var type = $(this).val();
		 	schoolid = type.split('-')[0];
		 	$.ajax({
		 		url: "<?=site_url('/user/getgrades')?>",
		 		type: 'post',
		 		data: {
		 			type: type.split('-')[1]
		 		},
		 		success: function(d){
		 			if(d.code){
		 				$('#grade').html(initOption(d.data))
		 				$('#class').html('')
		 			} else {
		 				console.log(d.msg)
		 			}
		 		},
		 		error: function(){

		 		},
		 		dataType: 'json'
		 	})
		 })

		 $('#grade').change(function(){
		 	var sid = $(this).val();
		 	var school = $('#school').val();
		 	$.ajax({
		 		url: "<?=site_url('/user/getclasses')?>",
		 		type: 'post',
		 		data: {
					school: school.split('-')[0],
					grade: sid
		 		},
		 		success: function(d){
		 			if(d.code){
		 				$('#class').html(initOption(d.data))
		 			} else {
		 				console.log(d.msg)
		 			}
		 		},
		 		error: function(){

		 		},
		 		dataType: 'json'
		 	})
		 })


		 function initOption(data){
		 	var tmp = '<option value="0">请选择</option>';
		 	for(var i= 0; i<data.length; i++){
		 		tmp += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
		 	}
		 	return tmp;
		 }


	});
</script>
